package com.pi.base.util;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.druid.util.StringUtils;
public class ValidUtil {
	/**
	 * 验证字符串是否空字符串（包括多个空字符串）
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str){
		if(str == null || str.trim().length() == 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 是否空的List
	 * @param list
	 * @return
	 */
	public static boolean isEmptyList(List<?> list){
		if(list == null || list.size() == 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 是否数字
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str){
		return null != str ? StringUtils.isNumber(str):false;
	}

	/**
	 * 驼峰式转换成下划线式。如custName-->cust_Name
	 * 
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {
		if (isBlank(param)) {
			return "";
		}
		Pattern pattern = Pattern.compile("[A-Z]");
		StringBuffer builder = new StringBuffer(param);
		Matcher mc = pattern.matcher(param);
		int i = 0;
		while (mc.find()) {
			builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group());
			i++;
			// 由于多个词组时，如meterSoftVersion对应的是meter_SoftVersion,故此处跳出循环。
			// break;
		}

		if ('_' == builder.charAt(0)) {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}


	public static boolean validateMobile(String mobile) {
		if (org.apache.commons.lang3.StringUtils.isBlank(mobile)) {
			return false;
		}
		Pattern pattern = Pattern.compile("^(1)\\d{10}$");
		Matcher matcher = pattern.matcher(mobile);
		if(!matcher.matches()){
			return false;
		}
		return true;
	}
}
